package com.hqyj.fj.javaSpringBoot.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.fj.javaSpringBoot.modules.account.dao.RoleResourceDao;
import com.hqyj.fj.javaSpringBoot.modules.account.dao.UserRoleDao;
import com.hqyj.fj.javaSpringBoot.modules.account.entity.Role;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.fj.javaSpringBoot.modules.account.dao.RoleDao;
import com.hqyj.fj.javaSpringBoot.modules.account.service.RoleService;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.fj.javaSpringBoot.modules.test.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;


@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    RoleResourceDao roleResourceDao;

    @Autowired
    RoleDao roleDao;

    @Override
    public Result<Role> insertRole(Role role) {
        LocalDateTime now = LocalDateTime.now();
        role.setCreateDate(now);
         roleDao.insertRole(role);
        return new Result<Role>(Result.ResultCode.SUCCESS.code,
                "Insert success",role);
    }

    @Override
    public Result<Role> updateRole(Role role) {
        roleDao.updateRole(role);
        return new Result<Role>(Result.ResultCode.SUCCESS.code,
                "update success",role);

    }

    @Override
    public Result<Role> deleteRole(int id) {

        userRoleDao.deleteUserRoleByRoleId(id);
        roleResourceDao.deleteRoleResourceByRoleId(id);
         roleDao.deleteRole(id);
        return new Result<Role>(Result.ResultCode.SUCCESS.code,
                "delete success");
    }

    @Override
    public Role getRoleById(int id) {
        return roleDao.getRoleById(id);
    }

    @Override
    public List<Role> getRoles() {
        return Optional.ofNullable(roleDao.getRoles()).orElse(Collections.emptyList());
    }

    @Override

    public PageInfo<Role> getRolesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();

        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Role>(Optional
                .ofNullable(roleDao.getRolesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public Result<Object> insertRoles(List<Role> roles) {

        roles.stream().forEach(item -> {
            item.setCreateDate(LocalDateTime.now());
        });

       roleDao.insertRoles(roles);
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "insert success.");
    }

    @Override
    public List<Role> getRolesByUserId(int userId) {
        return Optional.ofNullable(roleDao.getRolesByUserId(userId)).orElse(Collections.emptyList());
    }
}
